(function (fruit, util) {
    var NS = "http://www.w3.org/2000/svg";
    //var XLINK = 'http://www.w3.org/1999/xlink';


    fruit.define("fruit.util.scale",{
        properties:{
            input:{
                setter:function(range){
                    if(util.isArray(range)){
                        this._input = range;
                    }else{
                        this._input = [0,parseInt(range)];
                    }
                    this.rate("force");
                },
                getter:function(){
                    return this._input;
                }
            },
            output:{
                setter:function(range){
                    if(util.isArray(range)){
                        this._output = range;
                    }else{
                        this._output = [0,parseInt(range)];
                    }
                    this.rate("force");
                },
                getter:function(){
                    return this._output;
                }
            },
            rate:{
                setter:function(value){
                    var input = this.input();
                    var output = this.output();
                    if(input && output){
                        this._rate = (input[1] - input[0])/(output[1] - output[0]);
                    }
                },
                getter:function(){
                    return this._rate;
                }
            }
        },
        methods:{
            get:function(value){
                var input = this.input();
                var output = this.output();
                var rate = this.rate();
                //var rate =(input[1] - input[0])/(output[1] - output[0]);
                return (parseInt(value) - input[0])/rate + output[0];
            },
            invert:function(value){
                var input = this.input();
                var output = this.output();
                var rate = this.rate();
                return (parseInt(value) - output[0])*rate + input[0];
            }
        }
    });






    fruit.define("fruit.graphic.util", {
        singleton: true,
        methods: {
            create: function (type) {
                return d3.select(document.createElementNS(NS, type));
            }
        }
    });

    if (typeof(d3) != "undefined") {
        d3.selection.prototype.appendDOM = function (dom) {
            if (!(dom instanceof d3.selection)) {
                dom = d3.select(dom);
            }
            this.node().appendChild(dom.node());
            return dom
        }
    }


    fruit.ui.define("fruit.graphic.core", fruit.ui.Component, {
        view: {
            props: {
                'class': 'f-graphic',
                style: {
                    width: "{#width}",
                    height: "{#height}"
                }
            },
            content: [
                {
                    name: 'svg',
                    tag: 'svg',
                    props: {
                        'class': 'f-svg',
                        width: "{#width}",
                        height: "{#height}"
                    }
                }
            ]
        },
        properties: {
            width: {
                value: 0,
                observable: true
            },
            height: {
                value: 0,
                observable: true
            }
        },
        methods: {
            onInit:function() {
                if (typeof d3 != "undefined") {
                    this.$("svg")._element.setAttribute("svg_id", this.$id);
                    this.svg = d3.select(this.$("svg")._element);
                    // delete this.$view.__VIEW_ENGINE_REMARK__
                } else {
                    throw "Please include D3.js";
                }
            },
            setAll:function(opts){
                this.inherited(opts);
               // this.$("svg")._element.setAttribute("width", this.width());
                //this.$("svg")._element.setAttribute("height", this.height());
            }
        }
    });
})(fruit, fruit.util);